package com.air.admin.controller.imp;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.admin.controller.ISystemConfigManagerController;
import com.air.common.controller.imp.BaseControllerImp;
import com.air.common.mo.SystemConfig;
import com.air.common.service.ISystemConfigService;
import com.air.common.util.BasicAppConstant;
import com.air.common.vo.PageResultListVO;
import com.air.common.vo.ResponseVO;


@Controller
@RequestMapping("/admin/systemConfigManager")
public class SystemConfigManagerControllerImp extends BaseControllerImp
		implements ISystemConfigManagerController {
	
	@Autowired
	ISystemConfigService systemConfigService;
	
	@Override
	@RequestMapping("add")
	public ModelAndView addSystemConfig(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config) throws Exception {

		systemConfigService.addConfig(config);
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request,responseVO);
	}


	@Override
	@RequestMapping("list")
	public ModelAndView listSystemConfig(HttpServletRequest request,
			HttpServletResponse response) {
		
		List<SystemConfig> configs =  this.systemConfigService.getAllConfig();
		PageResultListVO resultVO = new PageResultListVO();
		resultVO.setTotalRecorderCount(configs.size());
		resultVO.setResult(configs);
		resultVO.setPageLimit(999999999);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject(BasicAppConstant._KEY_RESPONSE_VO,JSONObject.fromObject(resultVO).toString());
		mav.setViewName(BasicAppConstant._VIEW_COMMON_JSON);
		return mav;
		
	}

	@Override
	@RequestMapping("modify")
	public ModelAndView modifySystemConfig(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config) throws Exception{
		
		systemConfigService.updateConfigByKey(config);
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request,responseVO);
	}

	@Override
	@RequestMapping("remove")
	public ModelAndView removeSystemConfig(HttpServletRequest request,
			HttpServletResponse response, String keyList) throws Exception{
		
		List<String> sysConfKeyList = Arrays.asList(keyList.split(","));
		for(String key:sysConfKeyList){
			systemConfigService.deleteConfigByKey(key);
		}
		
		ResponseVO responseVO = new ResponseVO();
		return parse2Mav(request,responseVO);
		
	}

}
